<template>
  <div class="lottery-main">
    <el-collapse-transition>
      <div class="common-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model="listQuery.name" clearable placeholder="按抽奖名称进行搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <div class="common-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>
    </el-collapse-transition>
    <div class="btn-container">
      <div class="btn-title">
        <span>抽奖配置</span>
      </div>
      <div class="btn-btns">
        <el-button v-waves style="margin-left: 10px;" @click="handleCreate">新建</el-button>
        <el-button v-waves style="margin-left: 10px;" @click="handleDeletes">批量删除</el-button>
      </div>
    </div>
    <div class="table">
      <v-table
        :list="list"
        tag="lotteryconfig"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        @handleSelectionChange="handleSelectionChange"
      >
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handlePrize(scope.row)">设置奖品</el-button>
            <el-button type="text" size="mini" @click="handleStatus(scope.row)">{{ scope.row.status == 'BIGTSTATUS01' ? '暂停' : '开启' }}</el-button>
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </v-table>
    </div>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <v-prize v-if="showprize" :id="vid" :draw-form="drawForm" @cancel="handleCancel" />
  </div>
</template>

<script>
import { queryPage, del, update } from './../api/activity'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import VPrize from './../components/prize'

export default {
  name: 'LotteryMain',
  components: { Pagination, VTable, VPrize },
  directives: { waves },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      multipleSelection: [],
      showprize: false,
      vid: '',
      // 抽奖的类型
      drawForm: '',
      prizeCount:''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    filtercolumn(scope, item) {
      return scope.row[item]
    },

    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleReset() {
      this.listQuery.name = ''
      this.listQuery.page = 1
      this.getList()
    },

    handleStatus(row) {         
      const state = row.status === 'BIGTSTATUS01' ? 'BIGTSTATUS02' : 'BIGTSTATUS01'
      let msg = ''
      if (row.status === 'BIGTSTATUS01') {
        msg = '暂停'
      } else {
        msg = '开启'
      }
      this.prizeCount = row.prizeCount
      if(this.prizeCount === undefined){
        this.$message({
          message: '请设置奖品后再开启配置！',
          type: 'warning'
        });      
      }else{
        this.$confirm('确定' + msg + '该抽奖活动吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            update({ id: row.id, status: state })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: msg + '成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch(() => {})
      }      
    },

    handleCreate() {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'c' } })
    },

    handleUpdate(item) {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'u', i: item.id } })
    },

    handlePrize(row) {
      this.vid = row.id
      this.drawForm = row.drawForm
      this.showprize = true
    },

    handleCancel() {
      this.showprize = false
      this.getList()
    },

    handleDelete(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return new Promise((resolve, reject) => {
            del(id)
              .then(() => {
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                reject(error)
              })
          })
        })
        .catch(() => {})
    },

    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item) => {
          arr.push(item.id)
        })
        const ids = arr.join(',')
        this.$confirm('此操作将永久删除所选数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return new Promise((resolve, reject) => {
              del(ids)
                .then(() => {
                  this.$message({
                    title: '成功',
                    message: '删除成功',
                    type: 'success',
                    duration: 2000
                  })
                  this.getList()
                })
                .catch((error) => {
                  reject(error)
                })
            })
          })
          .catch(() => {})
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning'
        })
      }
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        bigturntableName: this.listQuery.name
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((response) => {
            this.list = response.data.data.rows
            this.total = response.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false            
          })
      })
    }
  }
}
</script>

<style lang="scss">
$dark_gray: #bfced6;
$mainColor: #18a5fd;
.lottery-main {
  padding: 10px;
  .common-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .common-search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .common-search-more {
        font-size: 14px;
        font-weight: 500;
        color: $mainColor;
        cursor: pointer;
        margin-left: 10px;
      }
    }
    &.merge {
      height: 50px;
      line-height: 50px;
    }
    &.separate {
      .common-search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor .el-input__inner {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .common-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .btn-container {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-top: 10px;
    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }
  .dataForm::-webkit-scrollbar {
    display: none;
  }
}
</style>
